<?php
class ControllerCommunityReport extends Controller{
    private $error=array();

    public function index(){
        $this->language->load('community/report');
        $this->language->load('community/community_header');

        if(isset($this->request->get['pid'])){
            $this->document->setTitle($this->language->get('report_post_heading_title'));
        }else{
            $this->document->setTitle($this->language->get('report_pm_heading_title'));
        }

        $this->load->model('community/report');

        //detect if user login
        if(!$this->customer->isLogged()){
            $this->redirect($this->url->link('account/login', '', 'SSL'));
        }

        //wrong url parameters
        if((!isset($this->request->get['pid']) && !isset($this->request->get['mid'])) || (isset($this->request->get['pid']) && isset($this->request->get['mid'])) || (isset($this->request->get['pid']) && (!isset($this->request->get['fid']) || !isset($this->request->get['tid'])))){
            $this->redirect($this->url->link('error/not_found'),'','SSL');
        }

        //alredy reported
        if($this->model_community_report->alreadyReported($this->request->get)){
            if(isset($this->request->get['pid'])){
                $this->session->data['forwarded_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'] . '&pid=' . $this->request->get['pid'] . '#' . $this->request->get['pid'],'SSL');
            }else{
                $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=ib&mid=' . $this->request->get['mid'],'SSL');
            }

            $this->session->data['warning_type']='already_reported';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //submit
        if(isset($this->request->post['submit']) && $this->otherReasonValidate()){
            $this->model_community_report->addReport($this->request->get,$this->request->post);

            unset($this->session->data['other_reason_id']);

            if(isset($this->request->get['pid'])){
                $this->session->data['forwarded_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'] . '&pid=' . $this->request->get['pid'] . '#' . $this->request->get['pid'],'SSL');
            }else{
                $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=' . $this->request->get['type'] . '&mid=' . $this->request->get['mid'],'SSL');
            }

            $this->session->data['success_type']='report';
            $this->redirect($this->url->link('community/success','','SSL'));
        }

        $this->data['breadcrumbs']=array();
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('common/home'),
            'text'      =>      $this->language->get('text_home'),
            'separator'=>      FALSE
        );
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/community'),
            'text'      =>      $this->language->get('text_community'),
            'separator' =>      $this->language->get('text_separator')
        );

        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/report'),
            'text'      =>      $this->language->get('text_report'),
            'separator' =>      $this->language->get('text_separator')
        );

        if(isset($this->request->get['pid'])){
            $this->data['heading_title']=$this->language->get('report_post_heading_title');
            $this->data['report_tip']=$this->language->get('report_post_tip');
        }else{
            $this->data['heading_title']=$this->language->get('report_pm_heading_title');
            $this->data['report_tip']=$this->language->get('report_pm_tip');
        }

        $this->data['text_notify_me_tip']=$this->language->get('text_notify_me_tip');
        $this->data['text_yes']=$this->language->get('text_yes');
        $this->data['text_no']=$this->language->get('text_no');
        $this->data['text_further_info_tip']=$this->language->get('text_further_info_tip');

        //entry
        $this->data['entry_reason']=$this->language->get('entry_reason');
        $this->data['entry_notify_me']=$this->language->get('entry_notify_me');
        $this->data['entry_further_info']=$this->language->get('entry_further_info');

        //button
        $this->data['button_submit']=$this->language->get('button_submit');
        $this->data['button_cancel']=$this->language->get('button_cancel');

        //error
        if(isset($this->error['reason'])){
            $this->data['error_reason']=$this->error['reason'];
        }else{
            $this->data['error_reason']='';
        }

        if(isset($this->request->get['pid'])){
            $this->data['cancel_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'] . '&pid=' . $this->request->get['pid'] . '#' . $this->request->get['pid'],'SSL');
        }else{
            $this->data['cancel_link']=$this->url->link('community/ucp','mode=pm&action=' . $this->request->get['type'] . '&mid=' . $this->request->get['mid'],'SSL');
        }

        $this->data['reasons']=array();

        $results=$this->model_community_report->getReportReasons();

        $i=0;

        foreach($results as $result){
            $this->data['reasons'][]=array(
                'reason_id'           =>      $result['reason_id'],
                'reason_description'  =>      $result['reason_description']
            );

            if(!isset($this->request->post['submit'])){
                $this->data['reasons'][$i]['selected']='';
            }else{
                if($result['reason_id']==$this->session->data['other_reason_id']){
                    $this->data['reasons'][$i]['selected']='selected="selected"';
                }else{
                    $this->data['reasons'][$i]['selected']='';
                }
            }

            $i++;
        }

        $this->session->data['other_reason_id']=$result['reason_id'];

		if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/community/report.tpl')){
            $this->template=$this->config->get('config_template') . '/template/community/report.tpl';
		}else{
            $this->template='default/template/community/report.tpl';
		}	
			
        $this->children = array(
            'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
		
		$this->response->setOutput($this->render()); 	
  	}

    private function otherReasonValidate(){
        if($this->request->post['report-reason']==$this->session->data['other_reason_id'] && empty($this->request->post['further-info'])){
            $this->error['reason']=$this->language->get('error_empty_further_info');
        }

        if(!$this->error){
            return true;
        }else{
            return false;
        }
    }
}
?>